#include "config.h"
#include "Arduino.h"

DHT dht(DHTPIN, DHTTYPE); // 创建DHT对象

void dhtSetup(){
    dht.begin();  // 初始化DHT传感器
    delay(500);
}

float* getDHTdata(){
    float *dhtData=new float[2];
    dhtData[0] = dht.readHumidity();    // 读取湿度值
    dhtData[1] = dht.readTemperature();  // 读取温度值（摄氏度）

    // 检查是否成功读取传感器数据
    if (isnan(dhtData[0]) || isnan(dhtData[1])) {
    Serial.println("Failed to read from DHT sensor!");
    return nullptr;
    }

    // 打印读取到的湿度和温度值
    Serial.print("Humidity: ");
    Serial.print(dhtData[0]);
    Serial.print("%  Temperature: ");
    Serial.println(dhtData[1]);
    return dhtData;
}

bool hasTempWarn=false,hasHumWarn=false;

void monitorDHT(){
    float *dht11Data=new float[2];
    dht11Data[0]=dht.readHumidity();    // 读取湿度值
    dht11Data[1]=dht.readTemperature();    // 读取温度值（摄氏度）
    #ifdef _SQUARELINE_PROJECT_UI_H
    lv_label_set_text(ui_ScWeTempAndHum, (String(int(dht11Data[1])) + "'C/" + String(int(dht11Data[0])) + "%").c_str());
    #endif
    if(!hasHumWarn){
        if(dht11Data[0]>53){
            Serial.println("hasHumWarn");
            I2S_play_file(sdRandomOpen("/highHumidity"));
            hasHumWarn=true;
        }else if (dht11Data[0]<27)
        {
            Serial.println("hasHumWarn");
            I2S_play_file(sdRandomOpen("/lowHumidity"));
            hasHumWarn=true;
        }else{
            ;
        }
    }
    if(!hasTempWarn){
        if(dht11Data[0]>53){
            Serial.println("hasTempWarn");
            I2S_play_file(sdRandomOpen("/highTemperature"));
            hasTempWarn=true;
        }else if (dht11Data[0]<27)
        {
            ;
        }else{
            ;
        }
    }

}

void updateMonitorDHT(){
    hasTempWarn=false;
    hasHumWarn=false;
}